﻿#include "ShareEventHandler.h"
#include <QDebug>
#include "IShareServiceController.h"
#include "IdeaShareDef.h"
#include "INotifyUI.h"

ShareEventHandler::ShareEventHandler(INotifyUI* eventHandler,ShareServiceController* controller)
{
    mUiEvtHandler = eventHandler;
    mshareController = controller;
}

ShareEventHandler::~ShareEventHandler() {}


void ShareEventHandler::OnError(ShareAppErr errorCode, std::string errorMsg, void* extralInfo)
{
    if (mUiEvtHandler != NULL) {
      mUiEvtHandler->HandleError(errorCode);
    }
}

void ShareEventHandler::OnDiscoverResult(DiscoverResult* discoverResult)
{
    qDebug() << "receive onDiscorver";
    if (mshareController == NULL) {
        qDebug() << "share controller is null";
        return;
    }
    mshareController->Connect(discoverResult->addressList,0, discoverResult->password);

}
void ShareEventHandler::OnDiscoverFailed(ShareAppErr errorCode, std::string errorMsg)
{
    qDebug() << "receive OnDiscoverFailed";
}

void ShareEventHandler::OnConnectResult(DeviceInfo* deviceInfo)
{
    qDebug() << "receive OnConnectResult";
    if (deviceInfo == NULL) {
        qDebug() << "deviceInfo is null, so return";
        return;
    }
    if (mUiEvtHandler != NULL) {
       mUiEvtHandler->HandleConnectResult(deviceInfo->deviceName);
    }
}
void ShareEventHandler::OnConnectFailed(ShareAppErr errorCode, std::string errorMsg)
{
    qDebug() << "receive OnConnectFailed";
}

void ShareEventHandler::OnDisconnectResult(int reason)
{
     qDebug() << "receive OnDisconnectResult";
     if (mUiEvtHandler != NULL) {
        mUiEvtHandler->HandleDisConnectResult();
     }
}
void ShareEventHandler::OnDisconnectFailed(ShareAppErr errorCode, std::string errorMsg)
{
    qDebug() << "receive OnDisconnectFailed";
}

void ShareEventHandler::OnSharePlayResult()
{
    qDebug() << "receive OnSharePlayResult";
     if (mUiEvtHandler != NULL) {
        mUiEvtHandler->HandleStartSharingResult();
     }
}
void ShareEventHandler::OnSharePlayFailed(ShareAppErr errorCode, std::string errorMsg)
{
    qDebug() << "receive OnSharePlayResult";
}

void ShareEventHandler::OnShareStopResult(int reason)
{
   qDebug() << "receive OnShareStopResult";
   if (mUiEvtHandler != NULL) {
      mUiEvtHandler->HandleStopSharingResult();
   }
}
void ShareEventHandler::OnShareStopFailed(ShareAppErr errorCode, std::string errorMsg)
{
    qDebug() << "receive OnShareStopFailed";
}

void ShareEventHandler::OnGetRemoteServiceStatusResult(RemoteServiceStatus *remoteServiceStatus)
{
   qDebug() << "receive OnGetRemoteServiceStatusResult";
   if (mUiEvtHandler != NULL) {
      mUiEvtHandler->HandleGetRemoteSericeStatusResult(remoteServiceStatus);
   }
}
void ShareEventHandler::OnGetRemoteServiceStatusFailed(ShareAppErr errorCode, std::string errorMsg)
{
    qDebug() << "receive OnGetRemoteServiceStatusFailed";
}

void ShareEventHandler::OnSetRemoteMicMuteResult(int result)
{
    qDebug() << "receive OnSetRemoteMicMuteResult";
    if (result != 0) {
       qDebug() << "receive set remote mic mute failed";
    }
}
void ShareEventHandler::OnSetRemoteMicMuteFailed(ShareAppErr errorCode, std::string errorMsg)
{
    qDebug() << "receive OnSetRemoteMicMuteFailed";
}

void ShareEventHandler::OnSetRemoteSpeakerVolumeResult(int result)
{
    qDebug() << "receive OnSetRemoteMicMuteResult";
}
void ShareEventHandler::OnSetRemoteSpeakerVolumeFailed(ShareAppErr errorCode, std::string errorMsg)
{
    qDebug() << "receive OnSetRemoteMicMuteFailed";
}

void ShareEventHandler::OnSetRemoteSpeakerMuteResult(int result)
{
   qDebug() << "receive OnSetRemoteSpeakerMuteResult";
   if (result != 0) {
      qDebug() << "receive set remote speaker mute failed";
   }
}
void ShareEventHandler::OnSetRemoteSpeakerMuteFailed(ShareAppErr errorCode, std::string errorMsg)
{
    qDebug() << "receive OnSetRemoteSpeakerMuteFailed";
}

void ShareEventHandler::OnSetRemoteCameraMuteResult(int result)
{
    qDebug() << "receive OnSetRemoteCameraMuteResult";
    if (result != 0) {
       qDebug() << "receive set remote camera mute failed";
    }
}
void ShareEventHandler::OnSetRemoteCameraMuteFailed(ShareAppErr errorCode, std::string errorMsg)
{
    qDebug() << "receive OnSetRemoteCameraMuteFailed";
}

void ShareEventHandler::OnSetLeaveConferenceResult(int result)
{
    qDebug() << "receive OnSetLeaveConferenceResult"<< result;
    if (mUiEvtHandler != NULL) {
       mUiEvtHandler->HandleLeaveConference();
    }
}
void ShareEventHandler::OnSetLeaveConferenceFailed(ShareAppErr errorCode, std::string errorMsg)
{
    qDebug() << "receive OnSetLeaveConferenceFailed";
}

void ShareEventHandler::OnSetEndConferenceResult(int result)
{
    qDebug() << "receive OnSetEndConferenceResult retult = " << result;
    if (mUiEvtHandler != NULL) {
       mUiEvtHandler->HandleEndConference();
    }
}
void ShareEventHandler::OnSetEndConferenceFailed(ShareAppErr errorCode, std::string errorMsg)
{
    qDebug() << "receive OnSetEndConferenceFailed";
}

void ShareEventHandler::OnGetSDKLatestVersionResult(std::string version)
{
    qDebug() << "receive OnGetSDKLatestVersionResult";
}
void ShareEventHandler::OnSetTLSFailed(ShareAppErr errorCode, std::string errorMsg)
{
    qDebug() << "receive OnSetTLSFailed";
}
void ShareEventHandler::OnSetMouseShapeResult(int result)
{
    qDebug() << "receive OnSetMouseShapeResult";
}
void ShareEventHandler::OnSetMouseShapeFailed(ShareAppErr errorCode, std::string errorMsg)
{
    qDebug() << "receive OnSetMouseShapeFailed";
}
void ShareEventHandler::OnGetSDKLatestVersionFailed(ShareAppErr errorCode, std::string errorMsg)
{
    qDebug() << "receive OnGetSDKLatestVersionFailed";
}

void ShareEventHandler::OnRemoteServiceStatusChangedNotify(RemoteServiceStatus *remoteServiceStatus, int index)
{
    qDebug() << "receive OnRemoteServiceStatusChangedNotify";
    if (mUiEvtHandler != NULL) {
        if (index == 1) {
            qDebug() << "receive OnUpdateRemoteMicMuteNotify";
            bool isMute = remoteServiceStatus->isMute;
            mUiEvtHandler->HandleUpdateMicMuteNotify(isMute);
        }
        else if (index == 2) {
            qDebug() << "receive OnUpdateRemoteCameraMuteNotify";
            bool isCameratMute = remoteServiceStatus->isCameraMute;
            mUiEvtHandler->HandleUpdateCameraMuteNotfiy(isCameratMute);
        }
        else if (index == 4) {
            qDebug() << "receive OnUpdateRemoteSpeakerVloumeNotify";
            int volume = remoteServiceStatus->volume;
            mUiEvtHandler->HandleUpdateSpeakerVolumeNotify(volume);
        }
        else if (index == 8) {
            qDebug() << "receive OnUpdateRemoteSpeakerMuteNotify";
            bool isSpeakerMute = remoteServiceStatus->isSpeakerMute;
            mUiEvtHandler->HandleUpdateSpeakerMuteNotify(isSpeakerMute);
        }
        else if (index == 16) {
            qDebug() << "receive OnUpdateRemoteChariManNotify";
            bool isChairMan = remoteServiceStatus->isChairman;
            mUiEvtHandler->HandleUpdateChairManNotify(isChairMan);
        }
    }

}

void ShareEventHandler::OnDevConfStateNotify(ConfSate confState, AuxState auxState)
{
   qDebug() << "receive OnDevConfStateNotify";
   if (mUiEvtHandler != NULL) {
       mUiEvtHandler->HandleDevConfStatusNotify(confState, auxState);
   }
}

void ShareEventHandler::OnShareStatusChangedNotify(ShareAppServerStatus status, ShareAppErr reason)
{
    if (mUiEvtHandler == NULL) {
      return;
    }
    if (status == SERVER_STATUS_STOP_SHARE) {
        qDebug() << "receive STOP SHARE";
        mUiEvtHandler->HandleServerStopShare(reason);

    }

    if (status == SERVER_STATUS_DISCONNECT) {
       qDebug() << "receive STOP connect";
        mUiEvtHandler->HandleServerDisconnect(reason);
    }

}
